package com.ubi.core.framework;

import com.ubi.core.framework.response.CheckIsExistsResponseBuilder;
import com.ubi.core.json.ObjectMapperCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.ArrayList;
import java.util.List;

public class DefaultSiteWebConfig extends DefaultWebConfig {

    @Override
    public List<HttpMessageConverter<?>> messageConverters() {

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
//        jsonConverter.setObjectMapper(ObjectMapperCreator.createSiteObjectMapper());

        ArrayList<MediaType> textTypes = new ArrayList<>();
        textTypes.add(MediaType.TEXT_PLAIN);
        textTypes.add(new MediaType("application", "*+json", GlobalConfig.CHARSET_UTF8));
        textTypes.add(new MediaType("application", "json", GlobalConfig.CHARSET_UTF8));
        // textTypes.add(new MediaType("application/json;charset=UTF-8"));
        // textTypes.add(new MediaType("application/json"));
        // add in 2016-03-20
        textTypes.add(MediaType.APPLICATION_FORM_URLENCODED);

        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
        stringConverter.setSupportedMediaTypes(textTypes);

        jsonConverter.setSupportedMediaTypes(textTypes);
        messageConverters.add(jsonConverter);
        // messageConverters.add(stringConverter);
        return messageConverters;
    }

    @Bean
    public CheckIsExistsResponseBuilder checkIsExistsResponseBuilder() {

        return new CheckIsExistsResponseBuilder();
    }
}
